﻿using System;
using jcs.Ifc.SSManager;
using System.IO;
using System.Runtime.InteropServices;

namespace jcs.Core.SSManager
{
    public class Snapshot : ISnapshot
    {

        public Snapshot(string snapShotName, string logicalFileName, string filePath, string extension, DateTime creationTime)
        {
            SnapshotName = snapShotName;
            LogicalFileName = logicalFileName;
            FilePath = filePath;
            FileExtension = extension;
            CreationTime = creationTime;
        }

        public Snapshot(string snapShotName, string filePath)
        {
            SnapshotName = snapShotName;
            FilePath = filePath;
        }

        public String SnapshotName { get; private set; }
        public String LogicalFileName { get; private set; }
        public DateTime CreationTime { get; private set; }
        public String FilePath { get; private set; }
        public string FileExtension { get; private set; }

        public double SparseFileSize
        {
            get { throw new NotImplementedException(); }
        }

        public double UsedDiskSpace { get { return GetCompressedFileSize();} }

        [DllImport("kernel32.dll", SetLastError = true, EntryPoint = "GetCompressedFileSize")]
        static extern uint GetCompressedFileSizeAPI(string lpFileName, out uint lpFileSizeHigh);

        private double GetCompressedFileSize()
        {

            double ReturnValue;
            uint high;
            uint low;
            low = GetCompressedFileSizeAPI(this.FilePath, out high);
            var error = Marshal.GetLastWin32Error();

            if (high == 0 && low == 0xFFFFFFFF && error != 0)
            { 
                ReturnValue = 0; 
            }
            else
            {
                var Val = (high << 32) + low;
                ReturnValue = (double)Val;
            }

            if (ReturnValue != 0) { ReturnValue = ((ReturnValue / 1024) / 1024); }

            return ReturnValue;

        }

    }
}
